package com.vitalfit.strategy.strategyImpl;


import com.vitalfit.model.dto.HandleDTO;
import com.vitalfit.model.entity.user.Trainer;
import com.vitalfit.model.vo.UserPageVO;
import com.vitalfit.repository.UserRepository;
import com.vitalfit.strategy.UserStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequiredArgsConstructor
public class QueryTrainerStrategy implements UserStrategy {

    private final UserRepository userRepository;


    @Override
    @Transactional(readOnly = true)
    public Object execute(HandleDTO handleDTO) {

        int page = (handleDTO.getPage() != null) ? handleDTO.getPage() : 0;
        int size = (handleDTO.getSize() != null) ? handleDTO.getSize() : 10;
        Page<Trainer> trainerPage;
        if (handleDTO.getStarRating() != null) {
            trainerPage = userRepository.findAllTrainersByStarRating(handleDTO.getStarRating(), PageRequest.of(page, size));

        } else {
            trainerPage = userRepository.findAllTrainers(PageRequest.of(page, size));
        }

        UserPageVO response = new UserPageVO();
        response.setContent(trainerPage.getContent());
        response.setTotalPages(trainerPage.getTotalPages());
        response.setTotalElements(trainerPage.getTotalElements());
        return response;

    }
}
